package com.hsoft.zhxy.app.demo_03_01.controller;

import com.alibaba.fastjson.JSONObject;
import com.hsoft.zhxy.common.exception.BizException;
import com.hsoft.zhxy.common.exception.ExcpCodeInfoEnum;
import com.hsoft.zhxy.common.result.ResultBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Api(tags = "demo_03_01 默认及自定义异常处理")
@Log4j2
@Validated
@RestController
@RequestMapping(value = "/demo_03_01/demoException/", produces = "application/json;charset=UTF-8")
public class Demo004ExceptionController {

    /**
     * 自动触发异常
     * @return JSONObject
     */
    @ApiOperation(value = "自动触发异常", notes = "自动触发异常")
    @GetMapping("autoTrigExeption")
    public JSONObject autoTrigException() {
        int a = 1;
        int b = 0;
        int c = a/b;
        if(log.isInfoEnabled()) log.info(a);
        return ResultBody.setData("");
    }

    /**
     * 自定义异常
     * @param param
     * @return JSONObject
     */
    @ApiOperation(value = "自定义异常", notes = "自定义异常")
    @GetMapping("customTrigException")
    public JSONObject customTrigException(String param) {
        if ("1".equals(param)) {
            if (log.isInfoEnabled()) log.info("参数校验成功！");
        } else {
            throw new BizException(ExcpCodeInfoEnum.USER_ERROR_A0130);
        }
        return ResultBody.setData(param);
    }

}
